package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.TempDS;
import com.west_traveler.entity.TempList;
import com.west_traveler.entity.entity_editor.SpecialInfo;
import com.west_traveler.entity.entity_editor.SpecialList;
import com.west_traveler.entity.entity_editor.SpecialListenList;
import com.west_traveler.utils.GetCirclePhoto;
import com.west_traveler.ximalayafm.PlayActivity;
import com.west_traveler.ximalayafm.R;
import com.west_traveler.ximalayafm.SpecialActivity;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Description: 精品听单二级接口RecyclerView适配器
 * Date:2016年12月07日10:21
 *
 * @author: 刘悦.
 */

public class SpecialRecyclerViewAdapter extends RecyclerView.Adapter<SpecialRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private SpecialListenList ds;
    private Bitmap target;
    private SpecialInfo info;
    private SpecialList specialList;

    public SpecialRecyclerViewAdapter(Context context, SpecialListenList ds) {
        this.context = context;
        this.ds = ds;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return R.layout.item_special_header;
        } else if (position == 1) {
            return R.layout.item_special_tag;
        } else if (position > 1 && position < ds.getList().size() + 2) {
            return R.layout.item_special_list;
        } else {
            return R.layout.item_special_leave_msg;
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(viewType, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        if (position == 0) {
            holder.tvSummaryTitle.setText(ds.getInfo().getTitle());
            holder.tvSummary.setText(ds.getInfo().getIntro());
            holder.tvEditorName.setText(ds.getInfo().getNickname());

            setCircleBitmapToImageVie(holder.ivEditorPhoto, ds.getInfo().getSmallLogo());

//            Picasso.with(context).load(ds.getInfo().getSmallLogo()).into(holder.ivEditorPhoto);

        } else if (position == 1) {

        } else if (position > 1 && position < ds.getList().size() + 2) {
            List<SpecialList> list = ds.getList();
            specialList = list.get(position - 2);
            holder.tvListTitle.setText(specialList.getTitle());
            holder.tvListEditor.setText(specialList.getNickname());
            holder.tvUpdateDate.setText(new SimpleDateFormat("yyyy-MM", Locale.getDefault()).format(new Date(specialList.getCreatedAt())));
            holder.tvPlayCounts.setText((specialList.getPlaysCounts() / 10000 + "万"));
            holder.tvPlayTime.setText(new SimpleDateFormat("mm:ss", Locale.getDefault()).format(new Date(specialList.getDuration() * 1000)));
            holder.tvLikeCounts.setText(String.valueOf(specialList.getSharesCounts()));
            holder.tvCommentCounts.setText(String.valueOf(specialList.getCommentsCounts()));

            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent();
                    intent.setClass(context, PlayActivity.class);
                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("ds", ds);
                    intent.putExtra("index", position - 2);
//                    intent.putExtra("smallLogo",ds.getInfo().getSmallLogo());
                    bundle.putBoolean("startService", true);

                    TempDS tempDS = new TempDS();
                    tempDS.setSmallLogo(ds.getInfo().getSmallLogo());

                    List<TempList> transDs= new ArrayList<>();
                    for (SpecialList list:ds.getList()) {
                        TempList tempList  = new TempList();

                        tempList.setNickName(list.getNickname());
                        tempList.setCoverLarge(list.getCoverSmall());
                        tempList.setTitle(list.getTitle());

                        transDs.add(tempList);
                    }

                    tempDS.setList(transDs);

                    List<SpecialList> list = ds.getList();
                    String[] playPathArray = new String[list.size()];
                    for (int i = 0; i < list.size(); i++) {
                        playPathArray[i] = list.get(i).getPlayPath32();
                    }
                    bundle.putStringArray("allPlaySource",playPathArray);
                    bundle.putParcelable("ds",tempDS);
                    intent.putExtra("playDataSource", bundle);
                    context.startActivity(intent);
                    ((SpecialActivity) context).overridePendingTransition(R.anim.down_in, android.R.anim.fade_out);
                }
            });

//            setCircleBitmapToImageVie(holder.ivSpecialPhoto, specialList.getCoverSmall());

//            Bitmap bitmap = setCircleBitmapToImageVie(specialList.getCoverSmall());
//            holder.ivSpecialPhoto.setImageBitmap(bitmap);

            Picasso.with(context).load(specialList.getCoverSmall()).into(holder.ivSpecialPhoto);
        } else {

            info = ds.getInfo();
            holder.tvListEditorNickname.setText(info.getNickname());
            holder.tvListEditorSign.setText(info.getPersonalSignature());

            setCircleBitmapToImageVie(holder.ivListEditorPhoto, info.getSmallLogo());

//            Picasso.with(context).load(info.getSmallLogo()).into(holder.ivListEditorPhoto);
        }

    }

    private void setCircleBitmapToImageVie(final ImageView view, final String imgUrl) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Bitmap bitmap = Picasso.with(context).load(imgUrl).get();
                    target = new GetCirclePhoto().transform(bitmap);
                    view.post(new Runnable() {
                        @Override
                        public void run() {
                            view.setImageBitmap(target);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }


//    private Bitmap setCircleBitmapToImageVie(final String imgUrl) {
//        new Thread() {
//            @Override
//            public void run() {
//                try {
//                    Bitmap bitmap = Picasso.with(context).load(imgUrl).get();
//                    target = new GetCirclePhoto().transform(bitmap);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//        return target;
//
//    }


    @Override
    public int getItemCount() {
        return ds.getList().size() + 3;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvSummaryTitle;
        private final TextView tvSummary;
        private final ImageView ivEditorPhoto;
        private final TextView tvEditorName;
        private final ImageView ivSpecialPhoto;
        private final TextView tvListTitle;
        private final TextView tvUpdateDate;
        private final TextView tvListEditor;
        private final TextView tvPlayCounts;
        private final TextView tvPlayTime;
        private final TextView tvLikeCounts;
        private final TextView tvCommentCounts;
        private final ImageView ivDownload;
        private final ImageView ivListEditorPhoto;
        private final TextView tvListEditorNickname;
        private final TextView tvListEditorSign;
        private final RelativeLayout relativeLayout;

        public ViewHolder(View itemView) {
            super(itemView);

            //头部简介部分
            tvSummaryTitle = ((TextView) itemView.findViewById(R.id.tv_special_title_id));
            tvSummary = ((TextView) itemView.findViewById(R.id.tv_summery_id));
            ivEditorPhoto = ((ImageView) itemView.findViewById(R.id.iv_summary_editor_photo_id));
            tvEditorName = ((TextView) itemView.findViewById(R.id.tv_special_editor_nickname_id));

            //听单列表部分
            ivSpecialPhoto = ((ImageView) itemView.findViewById(R.id.iv_special_photo_id));
            tvListTitle = ((TextView) itemView.findViewById(R.id.tv_special_title_id));
            tvUpdateDate = ((TextView) itemView.findViewById(R.id.tv_special_update_date_id));
            tvListEditor = ((TextView) itemView.findViewById(R.id.tv_special_editor_nickname_id));
            tvPlayCounts = ((TextView) itemView.findViewById(R.id.tv_special_play_count_id));
            tvPlayTime = ((TextView) itemView.findViewById(R.id.tv_special_play_time_id));
            tvLikeCounts = ((TextView) itemView.findViewById(R.id.tv_like_count));
            tvCommentCounts = ((TextView) itemView.findViewById(R.id.tv_special_comment_count_id));
            ivDownload = ((ImageView) itemView.findViewById(R.id.iv_special_download_id));
            relativeLayout = ((RelativeLayout) itemView.findViewById(R.id.rl_special_list_id));

            //尾部留言部分
            ivListEditorPhoto = ((ImageView) itemView.findViewById(R.id.iv_special_list_editor_photo_id));
            tvListEditorNickname = ((TextView) itemView.findViewById(R.id.tv_special_list_editor_nickname_id));
            tvListEditorSign = ((TextView) itemView.findViewById(R.id.tv_special_list_editor_sign_id));

        }
    }

}
